package com.book.service.impl;

import com.book.mapper.UserMapper;
import com.book.pojo.User;
import com.book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xuchangshuo
 * @date 2020/10/29 9:57
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean login(User user) {
        User user1 = userMapper.selectOne(user);
        if (user1 != null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean add(User user) throws Exception {
        //查询用户是否存在
        User user1 = userMapper.selectOne(user);
        //不为空  用户已存在
        if (user1 != null) {
            throw new RuntimeException("用户已经存在");
        }
        //不存在，添加用户
        int i = userMapper.insertSelective(user);
        System.out.println(i);
        //添加成功
        if (i != 0) {
            return true;
        }
        return false;
    }

    @Override
    public List findUserList() {
        List<User> users = userMapper.selectAll();
        return users;
    }

    @Override
    public boolean updateUser(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i != 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(int id) {
        int i = userMapper.deleteByPrimaryKey(id);
        if (i != 0) {
            return true;
        }
        return false;
    }

}
